'use strict';
import utils from '../../utils/utils';
import httpService from '../../utils/httpService';
import url from 'url';
import qs from 'querystring';
export default class extends think.service.base {
    /**
     * init
     * @return {}         []
     */
    init() {
        this.user = this.model('user');
        let tokenService=think.service('tokenservice');
        this.tokenService=new tokenService();
    }
    async addAdminUser(obj){
         try {

            let self = this;
            await this.user.startTrans();
            let user = await this.user.getUser({
                phone: obj.phone
            })
            if (!think.isEmpty(user)) {
                return 103;
            }

            let password = obj['password'];
            obj['roleid'] = 10;
            obj['password'] = utils().encrypt(password);
            let uId = await this.user.addUser(obj)
            await this.user.commit();
            // delete userInfo['password'];
            // userInfo['token']=this.tokenService.createToken(userInfo,secret);
            return uId;
        } catch (e) {
            await this.user.rollback();
            return 101;
        }
    }
    async getUser(obj,secret) {
        let self = this;
        let user = await this.user.getUser({
            phone: obj.phone
        });
        if (think.isEmpty(user)) {
            return 105;
        }
        let pwd = utils().decipher(user['password'])
        if (pwd !== obj['password']) {
            return 106;
        }
        delete user['password'];
        user['token']=this.tokenService.createToken(user,secret);
        return user;
    }

    async addUser(obj,secret) {
        try {
            let self = this;
            await this.user.startTrans();
            let user = await this.user.getUser({
                phone: obj.phone
            })
            if (!think.isEmpty(user)) {
                return 103;
            }
            let password = obj['password'];
            obj['roleid'] = 10;
            obj['password'] = utils().encrypt(password);
            let uId = await this.user.addUser(obj)
            let userInfo = await this.user.getUser({
                id: uId
            })
            await this.user.commit();
            delete userInfo['password'];
            userInfo['token']=this.tokenService.createToken(userInfo,secret);
            return userInfo;
        } catch (e) {
            await this.user.rollback();
            return 101;
        }

    }

    async sms(obj) {
        try {
            let utilsService = think.service('utils');
            let utils = new utilsService();
            let baiduSms = think.service('baidusms');
            let baidu = new baiduSms();
            let api = url.parse(baidu.get('baseurl'));
            let baiduModel = baidu.get('baiduModel');
            let ak = baidu.get('ak');
            let sk = baidu.get('sk');
            let authStringPrefix = baidu.get('authStringPrefix')
            let SigningKey = utils.SigningKey(sk, authStringPrefix);
            let CanonicalHeaders = {};
            for (let i in baiduModel) {
                if (i != "Content-Md5") {
                    CanonicalHeaders[i] = utils.enCode(baiduModel[i], true);
                } else {
                    CanonicalHeaders[i] = baiduModel[i];
                }
            }
            let dictHead = utils.lexicographicalOrder(CanonicalHeaders);
            let CanonicalURI = utils.enCode(api.path, false);
            let CanonicalRequest = "PUT" + "\n" + CanonicalURI + "\n" + dictHead;
            let Signature = utils.Signature(SigningKey, CanonicalRequest);
            let authString = authStringPrefix + '//' + Signature;
            let httpService = think.service('http');
            let http = new httpService();
            baiduModel['Authorization'] = authString;
            var rightNow = new Date();
            var res = rightNow.toISOString().slice(0, 10)
            baiduModel['x-bce-date'] = res;
            baiduModel['x-bce-content-sha256'] = utils.paramsEncode(baidu.get('model'));
            http.set({
                url: baidu.get('baseurl'),
                headers: baiduModel,
                body: JSON.stringify(baidu.get('model'))
            });
            await http.http((err, rsp, body) => {
                if (err) {
                    throw err;
                }
                console.log(rsp);
                console.log(body);
            })
            console.log('httpend')
        } catch (e) {
            console.log(e)
        }
    }
}
